google.load("feeds", "1");
google.load("jquery", "1.4.2");
google.load("jqueryui", "1.7.2");

var count = 0;
var feedURL = new Array();
var feedArray = new Array();
var intCurIndex = 0;
var searchText = "";


/*function loadPage() {
	initialize("http://www.thehindu.com/?service=rss", 'The Hindu');
	initialize("http://feeds.feedburner.com/NdtvNews-TopStories", 'NDTV');
	initialize("http://rss.cnn.com/rss/edition.rss", 'CNN');
	initialize(
			"http://feeds.bbci.co.uk/news/science_and_environment/rss.xml?edition=uk",
			'BBC');
	initialize("http://feeds.reuters.com/reuters/INtopNews", 'Reuters');
}*/

function getFeedObject(urlLink) {
	for ( var index = 0; index < feedURL.length; index++) {
		if (feedURL[index] == urlLink) {
			return index;
		}
	}
}

function getFeedContent(entry, feedObject) {
	var html = "<span class='header'>" + feedObject.header + "</span> ";
	html += '<p><a href="' + entry.link + '">' + entry.title + '</a></p>';
	html += entry.contentSnippet;
	html += "<span class='timeSpan' >" + new Date(entry.publishedDate)
			+ "</span>";
	html += '<div class="navMenu">'
	html += 'Displaying ' + (feedObject.nextIndex + 1) + ' of '
			+ feedObject.result.feed.entries.length;
	html += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a nohref style="cursor:pointer;color:blue;text-decoration:underline" onClick="moveNext('
			+ "'" + feedObject.url + "'" + ')"> >>  </a>';
	html += '&nbsp;&nbsp;&nbsp;<a nohref style="cursor:pointer;color:blue;text-decoration:underline" onClick="movePrev('
			+ "'" + feedObject.url + "'" + ')"> << </a></div>';
	return html;
}

function moveNext(urlLink) {
	var feedIndex = getFeedObject(urlLink);
	var feedObject = feedArray[feedIndex];
	var newContainer = document.getElementById(feedObject.divId);
	var nextIndex = feedObject.nextIndex;
	++nextIndex;
	if (nextIndex < feedObject.result.feed.entries.length) {
		var entry = feedObject.result.feed.entries[nextIndex];
		feedObject.nextIndex = nextIndex;
		feedArray[feedIndex] = feedObject;
		newContainer.innerHTML = getFeedContent(entry, feedObject);
	}
}

function movePrev(urlLink) {
	var feedIndex = getFeedObject(urlLink);
	var feedObject = feedArray[feedIndex];
	var newContainer = document.getElementById(feedObject.divId);
	var prevIndex = feedObject.prevIndex;
	var nextIndex = feedObject.nextIndex;
	if (nextIndex > 0) {
		prevIndex = nextIndex;
		--nextIndex;
		--prevIndex;
	}
	var entry = feedObject.result.feed.entries[prevIndex];
	feedObject.nextIndex = nextIndex;
	feedObject.prevIndex = prevIndex;
	feedArray[feedIndex] = feedObject;
	$(feedObject.divId).hide();
	newContainer.innerHTML = getFeedContent(entry, feedObject);
}

function searchFeeds() {
	var query = document.getElementById('txtsearch').value;
	searchText = query;
	google.feeds.findFeeds(query, findDone);
}

function findDone(result) {
	// Make sure we didn't get an error.
	if (!result.error) {
		var container = document.getElementById("slider1");
		var div = document.createElement("div");
		div.className = "floatMenu";
		div.id = "feed_search";
		var html = "<span class='header'> Search Results : " + searchText
				+ " </span> ";

		for ( var cnt = 0; cnt < result.entries.length; cnt++) {
			var entry = result.entries[cnt];
			html += '<p><a href="' + entry.link + '">' + entry.title
					+ '</a></p>';
			html += entry.contentSnippet;
		}
		div.appendChild(document.createTextNode(""));
		div.innerHTML = html;
		container.appendChild(div);
	}
}

function initialize(URL, header) {
	var feed = new google.feeds.Feed(URL);
	feed.setNumEntries(20);
	feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
	feed
			.load(function(result) {
				if (!result.error) {
					var container = document.getElementById("slider1");
					var div = document.createElement("div");
					div.className = "floatMenu";
					div.id = "feed_" + count;
					var html = "<span class='header'>" + header + "</span> ";
					var feedObject = new Object();
					feedObject.url = URL;
					feedObject.header = header;
					feedObject.result = result;
					feedObject.divId = "feed_" + count;
					feedObject.nextIndex = 0;
					feedObject.prevIndex = 0;
					feedArray[feedArray.length] = feedObject;
					feedURL[feedURL.length] = URL;
					var entry = result.feed.entries[0];
					var pdate = new Date(entry.publishedDate);
					html += '<p><a href="' + entry.link + '">' + entry.title
							+ '</a></p>';
					html += entry.contentSnippet;
					html += "<span class='timeSpan' >"
							+ new Date(entry.publishedDate) + "</span>";
					div.appendChild(document.createTextNode(""));
					div.innerHTML = html;
					container.appendChild(div);
					var navDiv = document.createElement("div");
					navDiv.className = "navMenu";
					navDiv.id = "navDiv";
					navDiv.appendChild(document.createTextNode(""));
					var dhtml = "";
					dhtml += 'Displaying ' + (feedObject.nextIndex + 1)
							+ ' of ' + feedObject.result.feed.entries.length;
					dhtml += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a nohref class="next" style="cursor:pointer;color:blue;text-decoration:underline" onClick="moveNext('
							+ "'" + URL + "'" + ')">>> </a>';
					dhtml += '&nbsp;&nbsp;<a nohref class="prev" style="cursor:pointer;color:blue;text-decoration:underline" onClick="movePrev('
							+ "'" + URL + "'" + ')"> << </a>';
					navDiv.innerHTML = dhtml;
					div.appendChild(navDiv);
					var emptyDiv = document.createElement("div");
					emptyDiv.className = "emptyDiv";
					emptyDiv.id = "emptyDiv";
					emptyDiv.appendChild(document.createTextNode(" "));
					container.appendChild(emptyDiv);
					++count;
				}
			});
}
